package com.yswl.pri;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yswl.pri.guide.AboutFragment;
import com.yswl.pri.guide.bean.TabEntity;
import com.yswl.pri.home.MovieChannelFragment;
import com.yswl.pri.fun.FunFragment;

import java.util.ArrayList;

import yswl.com.klibrary.base.MActivity;

public class MainActivity extends MActivity {


    public static void JumpAct(Activity context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        context.finish();
    }

    private static String[] TITLES = {"电影",/* "笑话",*/ "图片", "关于"};
    private static int[] ICONUNSELECTIDS = {
            R.mipmap.icon_movie_normal, /*R.mipmap.icon_joke_normal,*/
            R.mipmap.icon_picture_normal, R.mipmap.icon_more_normal};
    private static int[] ICONSELECTIDS = {
            R.mipmap.icon_movie_press, /*R.mipmap.icon_joke_press,*/
            R.mipmap.icon_picture_press, R.mipmap.icon_more_press};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    ViewPager mViewPager;
    CommonTabLayout mTabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTabData();

        mViewPager = findView(R.id.viewpager);
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
        mTabLayout = findView(R.id.tablayout);
        mTabLayout.setTabData(mTabEntities);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
//                if (position == 0) {
//                    mTabLayout.showMsg(0, 55);
//                    UnreadMsgUtils.show(mTabLayout_2.getMsgView(0), mRandom.nextInt(100) + 1);
//                }
            }
        });
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //两位数
//        mTabLayout.showMsg(0, 55);
//        mTabLayout.setMsgMargin(0, -5, 5);
//
//        //三位数
//        mTabLayout.showMsg(1, 100);
//        mTabLayout.setMsgMargin(1, -5, 5);
    }

    private void initTabData() {
        for (int i = 0; i < TITLES.length; i++) {
            mTabEntities.add(new TabEntity(TITLES[i], ICONSELECTIDS[i], ICONUNSELECTIDS[i]));
        }
        mFragments.add(new MovieChannelFragment());
        mFragments.add(new FunFragment());
//        mFragments.add(PictureTabLayoutFragment.newInstance());
        mFragments.add(new AboutFragment());
    }


    class MainPagerAdapter extends FragmentPagerAdapter {
        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }


    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addCategory(Intent.CATEGORY_HOME);
        startActivity(i);
    }
}
